﻿/********************************************************************************/
/* 	shot.d																		*/
/*------------------------------------------------------------------------------*/
/*	製作		（　゜ワ゜）ノ　／　松久貫太									*/
/*	製作開始　	2007/05/11														*/
/*	MAIL		omega@personal.email.ne.jp										*/
/*	URL		http://nagoya.cool.ne.jp/o_mega										*/
/*																				*/
/*	このソースは「やらわかライセンス」の元で配布されています。					*/
/*-更新履歴---------------------------------------------------------------------*/
/*	2007/07/30 製作開始															*/
/*-その他-----------------------------------------------------------------------*/
/*	なし																		*/
/********************************************************************************/

private import hell2;
private import actor;
private import vector;
private import screen;
private import player;
private import effect;
private import state;
private import std.math;

private import defines;

// 弾の種類
enum SHOTTYPE
{
	NORMAL,
	BREAK,
	ACCEL,
};
float SHOTSIZE[] = [0.2 , 0.2 , 0.2];

class ShotActor : ActorPool!(Shot)
{
public:
	GameMain gamemain;
	
	this(GameMain gm,int n)
	{
		gamemain = gm;
		super(n);
	}
	
	void fire(Vec3 p ,Vec3 v,SHOTTYPE ty)
	{
		Shot s = this.getInstance();
		if(s)
		{
			s.init(gamemain,p,v,ty);
		}
	}
	
	Shot getNearestShot(Vec3 v)
	{
		float dist = 99999999.0;
		Shot result = null;
		
		foreach(Shot s;actors)
		{
			if(s.exist)
			{
				float d = s.position.getDistSqrt(v);
				if(d < dist)
				{
					dist = d;
					result = s;
				}
			}
		}
		return result;
	}
	
	int countType(SHOTTYPE st)
	{
		int result = 0;
		foreach(Shot s;actors)
		{
			if(s.type == st && s.exist) result++;
		}
		return result;
	}
}

/* */
class Shot : Actor
{
public:
	Vec3 speed;
	Vec3 rotation;
	Vec3 rot_speed;
	SHOTTYPE type;
	int timer;
	GameMain gamemain;
	
	this()
	{
		super();
	}
	
	void init(GameMain gm,Vec3 p,Vec3 v,SHOTTYPE ty)
	{
		gamemain = gm;
		exist = true;
		position = new Vec3(p);
		speed = new Vec3(v);
		rotation = new Vec3( 0 ,0 , atan2(speed.y , speed.x) * 180 / 3.141592);
		rot_speed = new Vec3(
						Hell_randInt(-100,100) / 4.0 ,
						0,0 );
		/*
		rot_speed = new Vec3( Hell_randInt(-100,100) / 4.0 ,
						Hell_randInt(-100,100) / 4.0 ,
						Hell_randInt(-100,100) / 4.0 );
		*/
		type = ty;
		timer = 0;
		
		position.z = CHAR_Z_SURFACE;
	}
	
	void move()
	{
		position += speed;
		rotation += rot_speed;
		timer++;
		
		if(timer > 40) exist = false;
		/*
		if(position.x > CHAR_X_ABSMAX + 2 || position.x < -CHAR_X_ABSMAX - 2
			 || position.y > CHAR_Y_ABSMAX + 2 || position.y < -CHAR_X_ABSMAX - 2)
		{
			exist = false;
		}
		*/
	}
	
	void draw()
	{
		glPushMatrix();
		{
			glTranslatef(position.x , position.y ,position.z);
			glRotatef(0 , 0 , rotation.z);
			glRotatef(rotation.x ,rotation.y ,0);
			
			//float sz = getSize();
			glScalef(0.5 , 0.5 , 0.5);
			//glColor4f(0.8 , 1.0 , 1.0 , 0.4);
			glColor4f(0.1 , 0.6 , 1.0 , 0.6);
			NeedleShape.drawWire();
		}
		glPopMatrix();
		/*
		resetPerspective();
		Vec3 dist = new Vec3(gamemain.player.position - position);
		Hell_drawFont(std.string.toString(cast(int)(dist.getDist() * 100) / 100.0),
			cast(int)(320 + position.x * 15) ,
			cast(int)(240 + position.y * 10) ,0.5 );
		setPerspective();
		*/
	}
	
	float getSize()
	{
		return SHOTSIZE[type];
	}
	
	void destory()
	{
		exist = false;
		
		for(int t=0;t<16;t++){
			double r = Hell_randInt(0,100) / 200.0;
			double rx = Hell_randInt(-60,60) / 30.0 * Hell_randInt(-30,30) / 180.0 * 3.141592
					 + atan2(speed.y , speed.x) + 3.141592; 
			double ry = Hell_randInt(-60,60) / 180.0 * 3.141592; 
			gamemain.addEffect(position , 
				new Vec3(r * cos(rx) * cos(ry) , r * sin(rx) * cos(ry)  , r * sin(ry)) ,
				EFFECTTYPE.POINT);
		}
	}
}